﻿using Iworld.basis.Service;
using Iworld.Game.Manager;
using Iworld.Game.Service.Backstage.Export;
using Iworld.Game.Service.Backstage.Import;
using Iworld.Main.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Game.Service.Backstage
{
    /// <summary>
    /// 后台游戏服务
    /// </summary>
    public class GameService : IGameService
    {

        /// <summary>
        /// 获取全局抽水统计
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<ProfitStatisticsExport> GetProfitStatistics(GetProfitStatisticsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetProfitStatistics(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<ProfitStatisticsExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取个人抽水统计
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<ProfitStatisticsOfPersonExport> GetProfitStatisticsOfPerson(GetProfitStatisticsOfPersonImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetProfitStatisticsOfPerson(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<ProfitStatisticsOfPersonExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取投注记录
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<BettingRecordExport> GetBettingRecords(GetBettingRecordsImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetBettingRecords(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<BettingRecordExport>(ex.Message);
            }
        }

        /// <summary>
        /// 获取牌轮信息
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public PageResult<GameChapterExport> GetGameChaptersInfo(GetGameChaptersImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return import.GetGameChaptersInfo(db);
                }
            }
            catch (Exception ex)
            {
                return new PageResult<GameChapterExport>(ex.Message);
            }
        }

        /// <summary>
        /// 编辑牌局信息
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditGameRound(EditGameRoundImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                   import.CheckAllowExecuteOrNot(db);
                   new GameManager(db).Update(import);
                   return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 编辑游戏设置
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult EditGameSetting(EditSettingOfGameImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    SettingOfGame st = new SettingOfGame(db);
                    st.EachChapterRoundCount = import.EachChapterRoundCount;
                    st.EachRoundTimeSpan = import.EachRoundTimeSpan;
                    st.EachChapterTimeSpan = import.EachChapterTimeSpan;
                    st.LimitRedGolds = import.LimitRedGolds;
                    st.ClosureSingleTime = import.ClosureSingleTime;
                    st.Save(db);
                    return new NormalResult();
                }
            }
            catch (Exception ex)
            {
                return new NormalResult(ex.Message);
            }
        }

        /// <summary>
        /// 获取游戏设置
        /// </summary>
        /// <param name="import"></param>
        /// <returns></returns>
        public NormalResult<GameSettingExport> GetGameSetting(GetSettingOfGameImport import)
        {
            try
            {
                using (ModelToDbContext db = new ModelToDbContext())
                {
                    import.CheckAllowExecuteOrNot(db);
                    return new NormalResult<GameSettingExport>(new GameSettingExport(new SettingOfGame(db)));
                }
            }
            catch (Exception ex)
            {
                return new NormalResult<GameSettingExport>(null,ex.Message);
            }
        }


    }
}
